package com.tenglong.teacher.ui.adapter.viewholder;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.cuotibao.zhuoxiao.basics.GlobalConstant;
import com.cuotibao.zhuoxiao.basics.utils.DensityUtil;
import com.cuotibao.zhuoxiao.basics.utils.glide.GlideOptions;
import com.netease.nim.uikit.common.util.sys.TimeUtil;
import com.tenglong.teacher.R;
import com.tenglong.teacher.data.resultInfo.HomeDetailInfo;
import com.tenglong.teacher.ui.PhotoBrowserActivity;
import com.tenglong.teacher.ui.adapter.viewholder.base.BaseViewHolder;

/**
 * @author hdx on 2019/5/23.
 */
public class HomeworkContentViewHolder extends BaseViewHolder<HomeDetailInfo> {
    private TextView title, teacherHomeworkMessage;
    private ViewGroup imageList;
    private TextView tvHomeworkAudio;
    private ViewGroup homeworkAudio;
    private ImageView ivHomeworkAudio;

    public HomeworkContentViewHolder(View itemView) {
        super(itemView);
        initView();
    }

    private void initView() {
        title = itemView.findViewById(R.id.title);
        teacherHomeworkMessage = itemView.findViewById(R.id.teacherHomeworkMessage);
        imageList = itemView.findViewById(R.id.imageList);

        homeworkAudio = itemView.findViewById(R.id.homeworkAudio);
        ivHomeworkAudio = itemView.findViewById(R.id.iv_homeworkAudio);
        tvHomeworkAudio = itemView.findViewById(R.id.tv_homeworkAudio);
    }

    @Override
    public void setLayoutInfo(final HomeDetailInfo info) {
        title.setText("作业内容");
        title.setCompoundDrawablesWithIntrinsicBounds(R.drawable.zuoye, 0, 0, 0);

        homeworkAudio.setVisibility(TextUtils.isEmpty(info.audioUrl) || info.audioDuration == 0 ? View.GONE : View.VISIBLE);
        if (homeworkAudio.getVisibility() == View.VISIBLE) {
            long seconds = TimeUtil.getSecondsByMilliseconds(info.audioDuration);
            if (seconds >= 0) {
                tvHomeworkAudio.setText(seconds + "\"");
            } else {
                tvHomeworkAudio.setText("");
            }
        }

        teacherHomeworkMessage.setVisibility(TextUtils.isEmpty(info.answer) ? View.GONE : View.VISIBLE);
        if (teacherHomeworkMessage.getVisibility() == View.VISIBLE) {
            teacherHomeworkMessage.setText(info.answer);
        }

        imageList.setVisibility(info.imageUrls == null || info.imageUrls.size() == 0 ? View.GONE : View.VISIBLE);
        imageList.removeAllViews();
        if (imageList.getVisibility() == View.VISIBLE) {
            for (int i = 0; i < info.imageUrls.size(); i++) {
                if (((i + 1) % 3) == 1) {
                    LinearLayout linear = new LinearLayout(mContext);
                    imageList.addView(linear);
                    ViewGroup.LayoutParams params = linear.getLayoutParams();
                    params.width = ViewGroup.LayoutParams.MATCH_PARENT;
                    params.height = DensityUtil.dp2px(83);
                    if (params instanceof ViewGroup.MarginLayoutParams && imageList.getChildCount() != 1) {
                        ((ViewGroup.MarginLayoutParams) params).topMargin = DensityUtil.dp2px((float) 27.75);
                    }
                    linear.setLayoutParams(params);
                }
            }
            for (int i = 0; i < info.imageUrls.size(); i++) {
                ImageView imageView = new ImageView(mContext);
                ((ViewGroup) imageList.getChildAt(i / 3)).addView(imageView);
                ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
                layoutParams.height = DensityUtil.dp2px(83);
                layoutParams.width = DensityUtil.dp2px(83);
                if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
                    ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) layoutParams;
                    marginLayoutParams.leftMargin = DensityUtil.dp2px((float) 27.75);
                }
                imageView.setLayoutParams(layoutParams);
                RequestOptions options = GlideOptions.getGlideRequestOptions()
                        .placeholder(com.netease.nim.uikit.R.drawable.nim_image_default)
                        .error(com.netease.nim.uikit.R.drawable.nim_image_default);
                Glide.with(mContext)
                        .load(info.imageUrls.get(i))
                        .apply(options)
                        .into(imageView);
                imageView.setId(i);
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent i = new Intent();
                        i.setClass(mContext, PhotoBrowserActivity.class);
                        i.putExtra(GlobalConstant.KEY_IMAGE_URLS, info.imageUrls.toArray(new String[info.imageUrls.size()]));
                        i.putExtra(GlobalConstant.KEY_CURRENT_IMAGE_URL, info.imageUrls.get(v.getId()));
                        mContext.startActivity(i);
                        ((Activity) mContext).overridePendingTransition(0, 0);
                    }
                });
            }
        }


    }
}
